import 'package:flutter/material.dart';
import 'package:github_client_app/widget/MyListView.dart';

class OrdersPage extends StatefulWidget {
  const OrdersPage({Key? key}) : super(key: key);

  @override
  _OrdersPageState createState() => _OrdersPageState();
}

class _OrdersPageState extends State<OrdersPage>
    with AutomaticKeepAliveClientMixin {
  List<String> items = [];
  bool _hasMore = true;
  // final ScrollController _controller = ScrollController();

  // 下拉刷新
  Future<void> _onRefresh() async {
    await Future.delayed(const Duration(milliseconds: 500));
    setState(() {
      items = List.generate(20, (i) => "Item ${i + 1}");
      _hasMore = true;
    });
  }

  // 上拉加载更多
  Future<void> _loadMore() async {
    await Future.delayed(const Duration(milliseconds: 500));
    setState(() {
      items.addAll(List.generate(20, (i) => "Item ${i + 1}"));
      if (items.length > 50) {
        // 模拟没有数据了
        _hasMore = false;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // print("order build");
    return MyListView(
      // onRefresh: _onRefresh,  // 与reverse:true不能同时生效
      loadMore: _loadMore,
      reverse: true,
      hasMore: _hasMore,
      itemCount: items.length,
      itemBuilder: (context, index) {
        return Container(
          height: 100,
          alignment: Alignment.center,
          color: Colors.blue,
          child: Text(items[index]),
        );
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}
